<?php
namespace app\mobile\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $data=Db::table('lamp_txt')
            ->alias('txt')
            ->join('lamp_category cate','txt.cid=cate.id')
            ->field('txt.id,txt.desc,txt.photo,txt.name,txt.zuozhe,cate.cname')
            ->select();
//        dump($data);
        return view('list/list',['data'=>$data]);
    }

    public function mulu($id)
    {
        $data=db('txt')->where('id',$id)->find();
        $count = db('chapter')->where('bid',$id)->count('id');
        //
        $list = db('chapter')->where('bid',$id)->order('id','asc')->select();
//        dump($list);die;
        return view('mulu/mulu',['data'=>$data,'list'=>$list,'count' => $count]);
    }

    //第一次阅读
    public function showcontent($id)
    {
        $data = db('chapter')->where('id',$id)->find();
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        // var_dump($res);die;
        // var_dump($list);die;
        return view('content/index',['data4'=>$res4,'data'=>$data]);
    }

    //上一章
    public function showtop($id)
    {
        $lis = db('chapter')->where('id',$id)->find();
        $data = db('chapter')->where('id','<',$id)->order('id','desc')->where('bid',$lis['bid'])->find();
//        dump($data);die;
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        // $list = db('chapter')->where('id','<',$id)->order('id','asc')->find();
        // var_dump($list);die;
        if(!$data){
            $this->error('该书第一章');
        }
        return view('content/index',['data4'=>$res4,'data'=>$data]);
    }

    //下一章
    public function showdown($id)
    {
        $lis = db('chapter')->where('id',$id)->find();
        $data = db('chapter')->where('id','>',$id)->where('bid',$lis['bid'])->find();
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        // $list = db('chapter')->where('id','<',$id)->order('id','asc')->find();
        // var_dump($list);die;
        if(!$data){
            $this->error('该书最后章');
        }
        return view('content/index',['data4'=>$res4,'data'=>$data]);
    }


}
